<?php
/**
 * CRMLite Contact Controller
 *
 * @final
 * @package Crmlite_App
 * @version $Id: ContactController.php 59 2007-09-10 03:35:38Z etng2004 $
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
final class Admin_ContactController extends AdminBaseController
{
    /**
	 * 默认的模型
	 *
	 * 未初始化时是字符串，初始化之后就是一个模型接口
	 *
	 * @var ContactModel
	 */
    protected $model = 'Contact';

	public function init()
	{
		parent::init();
		$this->pushPoint('客户联系人管理');
	}

	public function indexAction()
	{
		$this->log('Contact Index page called!');
		$this->_forward('list');
	}

	protected function getLatestContact($count = 20)
	{
		$where = array(
		//		'expired_at = 0 OR DATE(FROM_UNIXTIME(expired_at)) <= CURDATE()',
		//		'published_at = 0 OR DATE(FROM_UNIXTIME(published_at)) >= CURDATE()',
		);
		$order = 'created_at DESC';
		return $this->model->findAll($where, $order, $count);
	}

	public function addAction()
	{
		if($this->isPost())
		{
			$record = $_POST['fields'];
			$record['created_at'] = time();
			if($this->model->insert($record))
			{
				$this->removeCache();
				$this->log($msg = "Contact #{$record['id']} created!");
				$this->feedback($msg, '成功', $this->module_url . '/client/view/id/' . $record['client_id']);
			}
			$this->view->contact = $record;
		}
		else
		{
			if($client = intval($this->_getParam('client', 0)))
			{
				$ClientModel = new ClientModel();
				$this->view->client = $ClientModel->find($client);
			}
			$this->view->contact = array(
			'client_id'=>$client
			);
		}

		$this->setTitle('新增客户联系人');
	}

	public function batchOperateAction()
	{
		/**
    	 * WE do not need render from template!
    	 */
		$this->_helper->viewRenderer->setNoRender();
		if($this->isPost())
		{
			var_dump($_POST);
			echo "todo";
		}
	}

	public function editAction()
	{
		if($this->isPost())
		{
			$record = $_POST['fields'];
			$id = $record['id'];
			$record['updated_at'] = time();
			if($this->model->update($record, "id={$id}"))
			{
                $record = $this->model->find($id);
                $this->model->asyncPrimaryContact($record['client_id']);
				$this->removeCache();
				$this->log($msg = "Contact #{$record['id']} updated!");
				$this->feedback($msg, '成功', $this->base_url);
			}
			$this->view->contact = $record;
			$error = 'Update failed!';
		}
		/**
    	 * 若不是POST方法则为初始编辑
    	 */
		$this->setTitle('修改客户联系人');
		if(empty($error))
		{
			$id = intval($this->_getParam('id', 0));
			$this->view->contact = $this->model->find($id);
		}
	}
	/**
     * 删除缓存
     *
     * 如果不指定记录ID，则默认删除所有相应的缓存
     *
     * @param int $id
     * @todo 实现删除所有的功能
     */
	protected function removeCache( $id = null )
	{
		$Cache = Zend_Registry::get('cache');
		/* @var $Cache Zend_Cache_Core */
		$Cache->remove('admin_Contact_feed_data');
		$Cache->remove('contact_feed_data');
	}
	/**
	 * 分页列举公告
	 *
	 */
	public function listAction()
	{
		$where = array(
		//		'expired_at = 0 OR DATE(FROM_UNIXTIME(expired_at)) <= CURDATE()',
		//		'published_at = 0 OR DATE(FROM_UNIXTIME(published_at)) >= CURDATE()',
		);
		if($client = intval($this->_getParam('client', 0)))
		{
			$where []= 'client_id = ' . $client;
			$ClientModel = new ClientModel();
			$this->view->client = $ClientModel->find($client);
		}

		$order = 'created_at DESC';
		$pager = array(
		'curpage' 	=> $this->_getParam('page', 1),
		'perpage'	=> $this->_getParam('perpage', 10),
		'total'		=> $this->model->count($where),
		'url'		=> $this->base_url . '/list',
		);
		$this->view->pager = $pager;
		$this->view->contacts = $this->model->paginate($where, $order, $pager['curpage'], $pager['perpage']);
		$this->setTitle('客户联系人管理', $pager['total']);
	}

	public function viewAction()
	{
		$id = intval($this->_getParam('id', 1));
		$this->view->contact = $this->model->find($id);
		$this->setTitle($this->view->contact['name']);
	}
	/**
	 * 设置首要联系人
	 *
	 */
	public function primaryAction()
	{
		$id = intval($this->_getParam('id', 0));
		$client = intval($this->_getParam('client', 0));
		if($id && $client)
		{
			if($status = $this->model->setPrimary($client, $id))
			{
				$this->removeCache($id);
				$this->log($msg = "Contact #{$id} Now is Primary  Contact of Client #{$client}!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		echo $status;
		exit();
	}
}
